// pages/cart/cart.js
let app = getApp();
let {
  myRequest
} = require("../../utils/myRequest")
let api = require("../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: false, //判断用户是否登录
    // allchecked: false, //全选或全不选


  },



  //用户未登录就跳转到登录页面
  gologin() {
    wx.navigateTo({
      url: '../../login/login'
    });
  },
  // 选择或取消选择商品
  changecheck(e) {
    // console.log("未选中绑定的值",e)
    let index = e.currentTarget.dataset.index
    console.log(index);
    let productIds = this.data.cartList[index].product_id
    let isChecked, price;
    let cartList = this.data.cartList
    let token = wx.getStorageSync('token');
    console.log(token);

    if (this.data.cartList[index].checked == 1) {
      console.log("aaaa");
      isChecked = 0

    } else if (this.data.cartList[index].checked == 0) {
      isChecked = 1

    }
    myRequest({
      url: api.checked,
      header: {
        'X-Nideshop-Token': token
      },
      method: "POST",
      data: {
        productIds: productIds,
        isChecked: isChecked
      },
      success: (res) => {
        console.log("选中某个商品", res)
        let cartList = res.data.data.cartList
        let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //全选的总价 
        let checkedGoodsCount = res.data.data.cartTotal.checkedGoodsCount //选择商品的总数
        // console.log("选择的返回的res",res)
        this.setData({
          cartList: cartList,
          checkedGoodsAmount: checkedGoodsAmount,
          checkedGoodsCount: checkedGoodsCount
        })

      }
    })
  },
  // 增加某个商品的数量
  addNum(e) {
    console.log(e);

    let index = e.currentTarget.dataset.index
    console.log(index);

    let productId = this.data.cartList[index].product_id //对应产品id
    console.log(productId);
    let goodsId = this.data.cartList[index].goods_id //对应商品id
    console.log("number", goodsId);

    let number = 0
    let token = wx.getStorageSync('token');
    if (goodsId, number, productId) {
      number = number + 1 //添加商品的个数
      myRequest({
        url: api.addCart,
        header: {
          'X-Nideshop-Token': token
        },
        method: "POST",
        data: {
          goodsId,
          number,
          productId
        },
        success: (res) => {
          console.log("商品数量加一", res)
          let cartList = res.data.data.cartList
          let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //加一个的价钱
          this.setData({
            cartList: cartList,
            checkedGoodsAmount: checkedGoodsAmount
          })
        }
      })
    }
  },
  // 减少某个商品的数量
  Toreduce(e) {
    console.log(e);

    let index = e.currentTarget.dataset.index
    console.log(index);

    let productId = this.data.cartList[index].product_id //对应产品id
    console.log(productId);
    let goodsId = this.data.cartList[index].goods_id //对应商品id
    // let number =this.data.cartList[index].number     //对应商品的数量
    let number = 0;
    console.log("number", number);

    let token = wx.getStorageSync('token');
    if (this.data.cartList[index].number > 1) { //当number(数量大于2就减1)


      number--; //添加商品的个数
      myRequest({
        url: api.addCart,
        header: {
          'X-Nideshop-Token': token
        },
        method: "POST",
        data: {
          goodsId,
          number,
          productId
        },
        success: (res) => {
          console.log("商品数量加一", res)
          let cartList = res.data.data.cartList
          let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //减一个的价钱
          this.setData({
            cartList: cartList,
            checkedGoodsAmount: checkedGoodsAmount
          })
        }
      })

    } else if (this.data.cartList[index].number <= 0) {
      return
    }
  },
  // 删除某个商品
  delcart(e) {
    console.log("未选中绑定的值", e)
    let index = e.currentTarget.dataset.index
    console.log(index);
    let token = wx.getStorageSync('token');
    let productIds = this.data.cartList[index].product_id
    console.log(productIds);

    // 2.删除购物车的商品
    myRequest({
      url: api.deletecart,
      header: {
        'X-Nideshop-Token': token
      },
      method: "POST",
      data: {
        productIds: productIds.toString()
      },
      success: (res) => {
        console.log("删除购物车商品", res)
        let cartList = res.data.data.cartList
        let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //全选的总价 
        cartList.splice(index, 1)
        console.log(cartList)
        this.setData({
          cartList: cartList,
          checkedGoodsAmount: checkedGoodsAmount

        })
      }
    })
  },
  // 全选
  Allchecked() {
    let token = wx.getStorageSync("token");
    let cartList = this.data.cartList
    var productIds = ""
    for (var index in cartList) { //遍历每个元素
      productIds += cartList[index].product_id + "," //将每个id用逗号拼接起来
      console.log(productIds)
    }
    if (this.data.checkedGoodsCount == 0) {
      wx.request({
        url: api.checked,
        method: "POST",
        data: {
          productIds: productIds,
          isChecked: 1
        },
        header: {
          'X-Nideshop-Token': this.data.token
        },
        success: (res) => {
          console.log(res);
          var cartList = res.data.data.cartList
          let checkedGoodsCount = res.data.data.cartTotal.checkedGoodsCount //选择商品的总数
          let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //全选的总价   
          this.setData({
            cartList: cartList,
            checkedGoodsCount: checkedGoodsCount,
            checkedGoodsAmount: checkedGoodsAmount
          })
        }
      })
    } else {
      wx.request({
        url: api.checked,
        method: "POST",
        data: {
          productIds: productIds,
          isChecked: 0
        },
        header: {
          'X-Nideshop-Token': token
        },
        success: (res) => {
          console.log(res);
          var cartList = res.data.data.cartList
          let checkedGoodsCount = res.data.data.cartTotal.checkedGoodsCount //选择商品的总数
          let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //全选的总价 
          this.setData({
            cartList: cartList,
            checkedGoodsCount: checkedGoodsCount,
            checkedGoodsAmount: checkedGoodsAmount
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 设置导航栏标题
    wx.setNavigationBarTitle({
      title: '购物车'
    })
    // 获取热门商品
    wx.request({
      url: api.index,
      success: (res) => {
        this.setData({
          goodsList: res.data.data.newGoodsList
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let token = wx.getStorageSync('token');

    //1. 获取购物车数据
    myRequest({
      url: api.cart,
      header: {
        'X-Nideshop-Token': this.data.token
      },
      success: (res) => {
        console.log("购物车列表", res)
        let cartList = res.data.data.cartList
        let checkedGoodsAmount = res.data.data.cartTotal.checkedGoodsAmount //全选的总价   
        let checkedGoodsCount = res.data.data.cartTotal.checkedGoodsCount //选择商品的总数

        console.log("选中的总价", checkedGoodsAmount);

        this.setData({
          cartList: cartList,
          checkedGoodsAmount: checkedGoodsAmount,
          checkedGoodsCount: checkedGoodsCount
        })
      }
    })
    // 判断用户是否已经登录
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})